<template>
  <div>
    <div>
      <el-input
          size="small"
          class="addPosInput"
          placeholder="添加职位"
          prefix-icon="el-icon-plus"
          @keydown.enter.native="addHandle"
          v-model="pos.name">
      </el-input>
      <el-button size="mini" icon="el-icon-plus" @click="addHandle" type="primary">添加</el-button>
    </div>
    <div>
      <el-table
          :data="positions"
          @selection-change="handleSelectionChange"
          style="width: 70%; margin-top: 10px;"
          stripe border
          size="small">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="编号" width="55"></el-table-column>
        <el-table-column prop="name" label="职位" width="200"></el-table-column>
        <el-table-column prop="create_date" label="创建时间" width="125"></el-table-column>
        <el-table-column prop="enabled" label="是否启用" width="150">
          <template slot-scope="scope">
            <el-tag size="small" v-if="scope.row.enabled" type="success">已启用</el-tag>
            <el-tag size="small" v-else="scope.row.enabled" type="danger">未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                size="small"
                @click="showEditView(scope.$index, scope.row)">编辑
            </el-button>
            <el-button
                size="small"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-button size="small" type="danger" style="margin-top: 8px" :disabled="this.posMultipleSelection.length==0"
               @click="deleteMultiple">批量删除
    </el-button>
    <el-dialog
        title="编辑职位"
        :visible.sync="dialogVisible"
        width="30%">
      <div>
        <div>
          <el-tag>职位名称</el-tag>
          <el-input v-model="updatePosName.name" size="small" class="updatePosName"></el-input>
        </div>
        <div style="margin-top: 5px">
          <el-tag>是否启用</el-tag>
          <el-switch
              style="margin-left: 8px"
              v-model="updatePosName.enabled"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="已启用"
              inactive-text="未启用">
          </el-switch>
        </div>


      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="doUpdate" size="small">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "PosMana",
  data() {
    return {
      pos: {
        name: ''
      },
      positions: [],
      dialogVisible: false,
      updatePosName: {
        name: '',
        enabled: false
      },
      posMultipleSelection: []
    }
  },
  mounted() {
    this.initPosition();
  },
  methods: {
    deleteMultiple() {
      this.$confirm('此操作将永久删除[' + this.multipleSelection.length + ']条职位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = '?';
        this.multipleSelection.forEach(item => {
          ids += 'ids=' + item.id + '&';
        })
        this.deleteRequest('/sys/basic/pos/' + ids).then(resp => {
          if (resp) {
            this.initPosition();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
    handleSelectionChange(val) {
      this.posMultipleSelection = val;
    },
    doUpdate() {
      this.putRequest('/sys/basic/pos/', this.updatePosName).then(resp => {
        if (resp) {
          this.initPosition();
          this.dialogVisible = false;
        }
      })
    },
    addHandle() {
      if (this.pos.name) {
        this.postRequest('/sys/basic/pos/', this.pos).then(resp => {
          if (resp) {
            this.initPosition();
            this.pos.name = ''
          }
        })

      } else {
        this.$message.error("职位名称不能为空")
      }
    },
    showEditView(index, row) {
      Object.assign(this.updatePosName, row)
      this.updatePosName.create_date = '';
      this.dialogVisible = true;

    },
    handleDelete(index, row) {
      this.$confirm('此操作将永久删除[' + row.name + ']职位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/sys/basic/pos/' + row.id).then(resp => {
          if (resp) {
            this.initPosition();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    initPosition() {
      this.getRequest('/sys/basic/pos/').then(resp => {
        if (resp) {
          this.positions = resp;
        }
      })
    }
  }
}
</script>

<style scoped>
.addPosInput {
  width: 220px;
  margin-right: 8px;
}

.updatePosName {
  width: 200px;
  margin-left: 8px;

}
</style>